package com.example.springboot.service.imp;

import com.example.springboot.bean.ProductRecord;
import com.example.springboot.bean.ShoppingRecord;
import com.example.springboot.dao.ProductRecordDao;
import com.example.springboot.dao.ShoppingRecordDao;
import com.example.springboot.dao.UserAccountDao;
import com.example.springboot.service.PurchaseBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.List;

/**
 * Created by tang on 2018/3/9.
 */
@Service
public class PurchaseBookServiceImpl implements PurchaseBookService {

    @Autowired
    private ProductRecordDao productRecordDao;

    @Autowired
    private ShoppingRecordDao shoppingRecordDao;

    @Autowired
    private UserAccountDao userAccountDao;


    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Override
    public void buyBook(ProductRecord productRecord, String customerName) {

        //1.商店商品数量减去1
        productRecordDao.updateBookStoreNum(1, productRecord.getBookName());

        //2.购买用户余额减去总价
        userAccountDao.updateUserAccount(productRecord.getBookPrice(), customerName);

        //3.购买记录表里面记录购买数据
        ShoppingRecord shoppingRecord = new ShoppingRecord(productRecord.getBookName(),
                customerName, new Date(new java.util.Date().getTime()));
        shoppingRecordDao.addShoppingRecord(shoppingRecord);
    }


}
